﻿function EnableContent(user, actionUrl) {
    var useTables = $("[id=Tables_" + user + "]:checked").val() !== undefined;
    var useBlobs = $("[id=Blobs_" + user + "]:checked").val() !== undefined;

    actionUrl = actionUrl.replace("_user_", user);
    actionUrl = actionUrl.replace("_useTables_", useTables);
    actionUrl = actionUrl.replace("_useBlobs_", useBlobs);

    $.ajax({ url: actionUrl, cache: false });
}

function SetPublic(table, actionUrl) {
    var isPublic = $("#Public_" + table + ":checked").val() !== undefined;

    actionUrl = actionUrl.replace("_table_", table);
    actionUrl = actionUrl.replace("_isPublic_", isPublic);

    $("input[name^='" + table + "'][type='checkbox']").each(function (ix, el) {
        el.disabled = isPublic;
    });

    $.ajax({ url: actionUrl, cache: false });
}

function SetPermission(table, user, addTablePermissionnActionUrl, removeTablePermissionActionUrl) {
    var hasAccess = $("[id=" + table + "_" + user + "]:checked").val() !== undefined;
    var actionUrl = '';

    if (hasAccess)
        actionUrl = addTablePermissionnActionUrl;
    else
        actionUrl = removeTablePermissionActionUrl;

    actionUrl = actionUrl.replace("_table_", table);
    actionUrl = actionUrl.replace("_user_", user);

    $.ajax({ url: actionUrl, cache: false });
}

function SendNotification(user, actionUrl) {
    var userId = user.toString().replace("#", "_").replace(".", "_");
    var message = $("#" + userId + "_Message").val();

    actionUrl = actionUrl.replace("_message_", message);
    actionUrl = actionUrl.replace("_user_", user);

    SetPushNotificationImageStatus(userId, "Sending");

    $.ajax({
        url: actionUrl,
        cache: false,
        success: function (data) {
            var msg = "";
            var failed = false;
            $.each(data, function (i, e) {
                if (e.Status != "Success") {
                    failed = true;
                    msg += "<img src=\"/Content/error_small.png\" alt=\"An error ocurred while sending push notification.\" />"
                    msg += " Device " + (i + 1) + " / " + data.length + " failed: " + e.Description + "<br />";
                } else {
                    msg += "<img src=\"/Content/success_small.png\" alt=\"Notification successfully sent.\" />"
                    msg += " Device " + (i + 1) + " / " + data.length + " success. <br />";
                }
            });
            if (failed) {
                SetPushNotificationImageStatus(userId, "Error", msg);
            }
            else {
                SetPushNotificationImageStatus(userId, "Success", msg);
            }
        },
        error: function () {
            SetPushNotificationImageStatus(userId, "Error", "Error accessing Push Notification Service");
        }
    });
}

function SetPushNotificationImageStatus(userId, status, tooltip) {
    if (status == "Sending") {
        $("#" + userId + "_Sending").show();
        $("#" + userId + "_Result").html("");
    }
    else {
        $("#" + userId + "_Sending").hide();
        $("#" + userId + "_Result").html(tooltip);
    }
}